﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Reflection;
using System.Windows.Media;

namespace GenTheme.Theme
{
    internal class Row : IRow
    {
        private IList<IThemeObject> tObjIList = new List<IThemeObject>();

        private ITheme Owner;

        public Row(List<Color?> lc, ITheme owner) : this(lc, owner, typeof(Row))
        {
        }

        public Row(List<Color?> lc, ITheme owner, Type type)
        {
            this.Owner = owner;
            foreach (Color? c in lc)
            {
                ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(Color?), typeof(IRow) });
                tObjIList.Add((IThemeObject)ci.Invoke(new object[2] { c, this }));
            }
        }

        #region IRow Members

        public void Add(IThemeObject item)
        {
            this.tObjIList.Add(item);
        }

        public bool IsLongRow
        {
            get
            {
                if (this.RowId < this.Owner.Count - 1)
                {
                    return this.Owner[this.RowId + 1].Count < ((IRow)this).Count;
                }
                else
                {
                    return this.Owner[this.RowId - 1].Count < ((IRow)this).Count;
                }
            }
        }

        public int RowId
        {
            get
            { 
                return this.Theme.IndexOf(this);
            }
        }

        public ITheme Theme
        {
            get
            {
                return this.Owner;
            }
        }

        #endregion

        #region Explicit IRow Members

        int IRow.IndexOf(IThemeObject item)
        {
            if (item != null)
            {
                return tObjIList.IndexOf(item);
            }
            return -1;
        }

        IThemeObject IRow.this[int index]
        {
            get
            {
                return this.tObjIList[index];
            }
        }

        bool IRow.Contains(IThemeObject item)
        {
            if (item != null)
            {
                return tObjIList.Contains(item);
            }
            return false;
        }

        int IRow.Count
        {
            get
            {
                return this.tObjIList.Count;
            }
        }

        #endregion

        #region IEnumerable<IThemeObject> Members

        IEnumerator<IThemeObject> IEnumerable<IThemeObject>.GetEnumerator()
        {
            return (IEnumerator<IThemeObject>)this.tObjIList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.tObjIList.GetEnumerator();
        }

        #endregion
    }
}
